﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Schema;
using BySoft.Server.Common.Enum;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Consts;
using OneSoft.EntityCore;

namespace BySoft.Web.Site.SysManage
{
    public partial class ParameterTypeAddOrEdit :  AuthBasePage
    {
     
        private const string OLDPARAMETERTYPENAME_FLAG = "OLDPARAMETERTYPENAME_FLAG";//旧参数类别名称标志

        #region ParameterTypeID
        /// <summary>
        ///  参数类别ID 
        /// </summary>
        protected String ParameterTypeID
        {
            get
            {
                if (ViewState[SysParametertypeTable.C_PARAMETERTYPEID] == null)
                {
                    String strID = String.Empty;
                    if (Request["ID"] != null)
                    {
                        //divheadtxt.InnerHtml = "<div class=\"breadcrumb\">系统管理 >> <a href="+"ParameterTypeList.aspx"+">参数类别</a>>>参数类别编辑</div>";
                        divheadtxt.InnerHtml = "<h1 class=\"tit\">系统管理 >> <a  href=" + "ParameterTypeList.aspx" + ">参数类别</a> >> 编辑</h1>";
                        strID = Request["ID"].ToString();
                        ViewState[SysParametertypeTable.C_PARAMETERTYPEID] = strID;
                    }
                    else
                    {
                        //divheadtxt.InnerHtml = "<div class=\"breadcrumb\">系统管理 >> <a  href="+"ParameterTypeList.aspx"+">参数类别</a>>>参数类别新增</div>";
                        divheadtxt.InnerHtml = "<h1 class=\"tit\">系统管理 >> <a  href=" + "ParameterTypeList.aspx" + ">参数类别</a> >> 新增</h1>";
                    }
                    return strID;
                }
                else
                {
                    return ViewState[SysParametertypeTable.C_PARAMETERTYPEID].ToString();
                }

            }
        }
        #endregion

        #region Event
        /// <summary>
        ///  页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!String.IsNullOrEmpty(ParameterTypeID))
                {
                    
                    LoadInfo(ParameterTypeID);
                }
                
            }
        }

        /// <summary>
        ///   确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOk_Click(object sender, EventArgs e)
        {
            //编辑
            if (!String.IsNullOrEmpty(ParameterTypeID))
            {
                if (ViewState[OLDPARAMETERTYPENAME_FLAG].ToString() != txtName.Text.Trim() &&
                    SysParametertype.IsNameExist(txtName.Text.Trim()))
                {
                    UIHelper.Alert(this.Page, String.Format(Message.Exists, "类别名称"));
                    return;
                }
                SysParametertypeEntity entity = new SysParametertypeEntity();
                entity.ParameterTypeID = ParameterTypeID;
                entity.ModifuUserID = this.CurrentUser.UserID;
                entity.ModifyDate = DateTime.Now;
                GetData(entity);
                if (SysParametertype.Update(entity))
                {
                    UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "/SysManage/ParameterTypeList.aspx");
                    base.LogAction(BaseEnum.ActionType.Modify, entity.ParameterTypeID, true, null, new EntityBase[] { entity });

                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    base.LogAction(BaseEnum.ActionType.Modify, entity.ParameterTypeID, false, null, new EntityBase[] { entity });
                }
            }
            else//新增
            {
                bool isExits = false;
                if (SysParametertype.IsIDExist(txtID.Text.Trim(), out isExits))
                {
                    UIHelper.AlertMessage(this.Page, String.Format(Message.Exists, "类别编号"));
                    return;
                }
                if (SysParametertype.IsNameExist(txtName.Text.Trim()))
                {
                    UIHelper.AlertMessage(this.Page, String.Format(Message.Exists, "类别名称"));
                    return;
                }
                SysParametertypeEntity typeEntity = GetEntity();
                bool bResult = false;
                if (isExits)
                {
                    if (SysParametertype.Update(typeEntity))
                    {
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "/SysManage/ParameterTypeList.aspx");
                        bResult = true;
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                    base.LogAction(BaseEnum.ActionType.Modify, typeEntity.ParameterTypeID, bResult, null, new EntityBase[] { typeEntity });
                }
                else
                {
                    if (SysParametertype.Insert(GetEntity()))
                    {
                        bResult = true;
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "/SysManage/ParameterTypeList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);

                    }
                    base.LogAction(BaseEnum.ActionType.Add, typeEntity.ParameterTypeID, bResult, null, new EntityBase[] { typeEntity });
                }
            }

        }

        #endregion

        #region method
        /// <summary>
        ///  根据页面输入获得新增实体
        /// </summary>
        /// <param name="entity"></param>
        private SysParametertypeEntity GetEntity()
        {
            SysParametertypeEntity entity = new SysParametertypeEntity();
            entity.CreateUserID = this.CurrentUser.UserID;
            entity.CreaterName = this.CurrentUser.UserName;
            entity.CreateDate = DateTime.Now;
            entity.ParameterTypeID = txtID.Text.Trim();
            entity.ModifyDate = DateTime.Now;
            entity.ModifuUserID = this.CurrentUser.UserID;
            entity.ModifyerName = this.CurrentUser.UserName;
            GetData(entity);
            return entity;
        }

        private void GetData(SysParametertypeEntity entity)
        {
            entity.Description = txtRemark.Text.Trim();
            entity.ParameterTypeName = txtName.Text.Trim();
            if (cbIsSolid.Checked)
            {
                entity.IsSolidify = ((int)BaseEnum.IsSolidify.Yes).ToString();
            }
            else
            {
                entity.IsSolidify = ((int)BaseEnum.IsSolidify.No).ToString();
            }

            entity.Status = ((int)BaseEnum.CommonSatus.Started).ToString();
        }
        /// <summary>
        ///  显示类别信息
        /// </summary>
        /// <param name="strCode">类别代号</param>
        private void LoadInfo(string strID)
        {
            SysParametertypeEntity entity = BySoft.Server.Business.SelectionCatche.GetParameterTypeByID(strID);
            if (entity != null)
            {
                spanEditID.Visible = false;
                lblID.Visible = true;
                lblID.Text = entity.ParameterTypeID;
                txtName.Text = entity.ParameterTypeName;
                ViewState[OLDPARAMETERTYPENAME_FLAG] = entity.ParameterTypeName;//缓存参数名称
                txtRemark.Text = entity.Description;
                if (entity.IsSolidify == ((int)BaseEnum.IsSolidify.Yes).ToString())
                {
                    cbIsSolid.Checked = true;
                    cbIsSolid.Enabled = false;
                }
                //创建信息
                trCreate.Visible = true;
                lblCreateTime.Text =entity.CreateDate==null?"": entity.CreateDate.Value.ToString(SystemConfig.DateFromat);
                lblCreator.Text = entity.CreaterName;

                //修改信息
                trModify.Visible = true;
                lblModifyTime.Text =entity.ModifyDate==null?"": entity.ModifyDate.Value.ToString(SystemConfig.DateFromat);
                lblModifyUser.Text = entity.ModifyerName;

            }
            else
            {
                UIHelper.AlertMessageGoToURL(this.Page, "找不到相关的参数类别", "/SysManage/ParamerterTypeList.aspx");
            }
        }
        #endregion

        }
    }

